﻿using Biz.EF.Gateway.Monitor;
using Biz.EF.Gateway.Total;
using Biz.EF.Platform.Gxt;
using Hjt212Parser;
using log4net;
using System;
using System.Linq;
using System.Threading;

namespace Biz.Process
{
    public class CommonProcessor
    {
        private static ILog Log = LogManager.GetLogger(typeof(CommonProcessor));

        public static void ProcessNonHourOrDay()
        {
            using (var gCtx = new GatewayMonitorContext())
            using (var gtCtx = new GatewayTotalContext())
            using (var pCtx = new PlatformGxtContext())
            {
                var currentThread = Thread.CurrentThread.ManagedThreadId;
                pCtx.Database.CommandTimeout = 300;

                var sql = "update received_msg set processed_flag='ProcessingFinished' where cn='2072'";
                gCtx.Database.ExecuteSqlCommand(sql);

                var flag = ProcessFlag.NotProcessed.ToString();
                var items =
                    gCtx.received_msg.Where(t => t.is_length_right)
                        .Where(t => t.processed_flag.Equals(flag) && !(t.cn.Equals("2061") || t.cn.Equals("2031")))
                        .OrderBy(t => t.received_datetime)
                        .Take(1000)
                        .ToList();
                var timeBegin = DateTime.Now;
                int count = 0;
                Log.Info(currentThread + "->" + " processing records begin." + items.Count);
                foreach (var item in items)
                {
                    var oneItemBegin = DateTime.Now;
                    Log.Info(currentThread + "->"+ " processing " + item.cn + ":" + item.received_string);
                    switch (item.cn)
                    {
                        case "2011":
                            ProcessorData4Monitor.Process2011(pCtx, item.received_string);
                            ProcessorData4Total.Process2011(pCtx, item.received_string);
                            ProcessorStatus.Process9999(pCtx, item.received_string);
                            break;
                        case "2051":
                            ProcessorData4Monitor.Process2051(pCtx, item.received_string);
                            ProcessorData4Total.Process2051(pCtx, item.received_string);
                            break;
                        case "2061":
                            ProcessorData4Monitor.Process2061(pCtx, item.received_string);
                            // var now = DateTime.Now;
                            // Log.Info("在线小时数据处理耗时秒:" + (now - begin).TotalSeconds);
                            // begin = now;
                            ProcessorData4Total.Process2061(pCtx, item.received_string);
                            // now = DateTime.Now;
                            // Log.Info("总量小时数据处理耗时秒:" + (now - begin).TotalSeconds);
                            // begin = now;
                            ProcessorSummation4Total.Process2061Sum2Day(pCtx, item.received_string);
                            // now = DateTime.Now;
                            //Log.Info("统计小时到日数据耗时秒:" + (now - begin).TotalSeconds);
                            // begin = now;
                            ProcessorDownlineData4Total.GenDownlineData2081(gtCtx, pCtx, item.received_string);
                            //now = DateTime.Now;
                            //Log.Info("生成下发指令耗时秒:" + (now - begin).TotalSeconds);
                            //begin = now;
                            ProcessorSummation4Total.Process2061Sum2TotalEmission(pCtx, item.received_string);
                            //var now = DateTime.Now;
                            break;
                        case "2031":
                            ProcessorData4Monitor.Process2031(pCtx, item.received_string);
                            ProcessorData4Total.Process2031(pCtx, item.received_string);
                            ProcessorSummation4Monitor.Process2031Sum2Month(pCtx, item.received_string);
                            ProcessorSummation4Monitor.Process2031Sum2Year(pCtx, item.received_string);
                            ProcessorSummation4Total.Process2031Sum2Month(pCtx, item.received_string);
                            ProcessorSummation4Total.Process2031Sum2Year(pCtx, item.received_string);
                            break;
                        case "0000":
                            ProcessorStatus.Process0000(pCtx, item.received_string);
                            break;
                        case "2072":
                            ProcessorAlarm.Process2072(pCtx, item.received_string);
                            break;
                        case "9999":
                            ProcessorStatus.Process9999(pCtx, item.received_string);
                            break;
                    }
                    item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                    Log.Info(currentThread + "->"+ ++count + " / " + items.Count + ": " + (DateTime.Now - oneItemBegin).TotalSeconds);
                    gCtx.SaveChanges();
                }
                Log.Info(currentThread + "->"+"Processing records end," + items.Count + ",seconds:" + (DateTime.Now - timeBegin).TotalSeconds);
            }
        }

        public static void ProcessHourAndDay()
        {
            using (var gCtx = new GatewayMonitorContext())
            using (var gtCtx = new GatewayTotalContext())
            using (var pCtx = new PlatformGxtContext())
            {
                var currentThread = Thread.CurrentThread.ManagedThreadId;

                pCtx.Database.CommandTimeout = 300;


                var flag = ProcessFlag.NotProcessed.ToString();
                var items =
                    gCtx.received_msg.Where(t => t.is_length_right)
                        .Where(t => t.processed_flag.Equals(flag) && (t.cn.Equals("2061") || t.cn.Equals("2031")))
                        .OrderBy(t => t.received_datetime)
                        .Take(1000)
                        .ToList();
                var timeBegin = DateTime.Now;
                int count = 0;
                Log.Info(currentThread + "->"+"Processing records begin." + items.Count);
                foreach (var item in items)
                {
                    var oneItemBegin = DateTime.Now;
                    Log.Info(currentThread + "->" + "Processing " + item.cn + ":" + item.received_string);
                    switch (item.cn)
                    {
                        case "2011":
                            ProcessorData4Monitor.Process2011(pCtx, item.received_string);
                            ProcessorData4Total.Process2011(pCtx, item.received_string);
                            ProcessorStatus.Process9999(pCtx, item.received_string);
                            break;
                        case "2051":
                            ProcessorData4Monitor.Process2051(pCtx, item.received_string);
                            ProcessorData4Total.Process2051(pCtx, item.received_string);
                            break;
                        case "2061":
                            ProcessorData4Monitor.Process2061(pCtx, item.received_string);
                            // var now = DateTime.Now;
                            // Log.Info("在线小时数据处理耗时秒:" + (now - begin).TotalSeconds);
                            // begin = now;
                            ProcessorData4Total.Process2061(pCtx, item.received_string);
                            // now = DateTime.Now;
                            // Log.Info("总量小时数据处理耗时秒:" + (now - begin).TotalSeconds);
                            // begin = now;
                            ProcessorSummation4Total.Process2061Sum2Day(pCtx, item.received_string);
                            // now = DateTime.Now;
                            //Log.Info("统计小时到日数据耗时秒:" + (now - begin).TotalSeconds);
                            // begin = now;
                            ProcessorDownlineData4Total.GenDownlineData2081(gtCtx, pCtx, item.received_string);
                            //now = DateTime.Now;
                            //Log.Info("生成下发指令耗时秒:" + (now - begin).TotalSeconds);
                            //begin = now;
                            ProcessorSummation4Total.Process2061Sum2TotalEmission(pCtx, item.received_string);
                            //var now = DateTime.Now;
                            break;
                        case "2031":
                            ProcessorData4Monitor.Process2031(pCtx, item.received_string);
                            ProcessorData4Total.Process2031(pCtx, item.received_string);
                            ProcessorSummation4Monitor.Process2031Sum2Month(pCtx, item.received_string);
                            ProcessorSummation4Monitor.Process2031Sum2Year(pCtx, item.received_string);
                            ProcessorSummation4Total.Process2031Sum2Month(pCtx, item.received_string);
                            ProcessorSummation4Total.Process2031Sum2Year(pCtx, item.received_string);
                            break;
                        case "0000":
                            ProcessorStatus.Process0000(pCtx, item.received_string);
                            break;
                        case "2072":
                            ProcessorAlarm.Process2072(pCtx, item.received_string);
                            break;
                        case "9999":
                            ProcessorStatus.Process9999(pCtx, item.received_string);
                            break;
                    }
                    item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                    Log.Info(currentThread + "->"+ (++count) + "/" + items.Count + ": " + (DateTime.Now - oneItemBegin).TotalSeconds);
                    gCtx.SaveChanges();
                }
                Log.Info(currentThread + "->" + "Processing records end," + items.Count + ",seconds:" + (DateTime.Now - timeBegin).TotalSeconds);
            }
        }
    }
}
